use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;

mod grep_rs;
mod player;
mod copy_rs;

fn main() ->io::Result<()> {
    println!("Hello, world!");
    // // * 表示下一个按指定的格式输出
    // grep_rs::grep("ERROR").unwrap();
    // // _print_log_file();

    // let result = grep_rs::grep_main();
    // if let Err(err) = result {
    //     eprintln!("Error: {}", err);
    //     std::process::exit(1);
    // }

    let mut player = player::Player::new("player1");
    println!("player = {:?}", player);
    player.items.push("sword".to_string());
    player.items.push("shield".to_string());
    player.items.push("potion".to_string());
    player.score += 100;
    println!("player = {:?}", player);
    player.health -= 10;
    println!("player = {:?}", player);
    serde_json::to_writer(std::io::stdout(), &player).unwrap();
    println!("");
    let file_name ="C:\\Users\\Administrator\\Downloads\\app.log";
    let file_path = Path::new(file_name);
    for p in file_path.ancestors() {
        println!("p = {:?}", p);
        
    }
    let file_path = Path::new("C:\\Users\\Administrator\\Downloads");
    for entry_res in file_path.read_dir()? {
        let entry = entry_res?;
        let path = entry.path();
        let meta_data = entry.metadata()?;

        println!("path = {:?}", path);
        println!("meta_data = {:?}", meta_data);
    }

    Ok(())

}



fn _print_log_file() {
    let file_name ="C:\\Users\\Administrator\\Downloads\\app.log";
    // 读取出指定文件中的堆栈异常信息
    let file = File::open(file_name).unwrap();
    let reader = io::BufReader::new(file);
    let mut exception = false;
    for line in reader.lines() {
        let line = line.unwrap();
        if line.starts_with("[") {
            exception = false;
        }
        if line.contains("Exception") {
            // println!("{}", line);
            exception = true;
        }

        if exception {
            println!("{}", line);
        }



    }
}
